﻿using MyServer.Common;
using MyServer.Udp;
using ScreenMonitor.Common;
using ScreenMonitor.Common.Objects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ScreenMonitor.ServerLib
{
    public static class UdpCommandOperater
    {
        #region========私有变量========

        private static object LockObj = new object();
        private static object SendLockObj = new object();
        internal static UdpClient LocalUdpClient;

        #endregion

        public static bool RunCommand(DataPackageCollection pkg)
        {
            lock (LockObj)
            {
                try
                {
                    string command = Util.GetCommandLine(pkg.PackageName);
                    switch (command)
                    {
                        case CommandType.ClientStarted:
                            return DoClientStart(pkg);
                        default:
                            return false;
                    }
                }
                catch
                {
#if DEBUG
                throw;
#else
                    return false;
#endif
                }
            }
        }

        private static bool DoClientStart(DataPackageCollection pkg)
        {
            UdpPkgInfo info = (UdpPkgInfo)pkg.Tag;

            AcceptConnectObject obj = new AcceptConnectObject(
                CommandObjects.TcpCenter.BindEndPoint.Port,
                ServerGlobalSettingScreenViewImg.ScreenImgHeight,
                ServerGlobalSettingScreenViewImg.ScreenImgWidth,
                ServerGlobalSettingScreenViewImg.ImageType,
                ServerGlobalSettingScreenViewImg.SendBmpRate,
                ServerGlobalSettingScreenViewImg.AutoStartSendBmp,
                ServerGlobalSettingScreenViewImg.ImageSizeMode
                );

            CommandObjects.UdpCommandCenter.SendCommandWithData(CommandType.AcceptConnect, obj.ToByte(), info.BindPoint);
            return true;
        }

    }
}
